﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Linq;
using DTO;

namespace DAO
{
    public class PRODUCTDAO
    {
        public static PRODUCT[] LayBang()
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var products = db.usp_SelectProductsAll();            
            return products.ToArray<PRODUCT>();
        }

        public static PRODUCT[] DanhSachSanPhamTheoThanhPho(int idCity)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var products = db.usp_SelectProductsByCity(idCity);
            return products.ToArray<PRODUCT>();
        }

        public static PRODUCT[] DanhSachSanPhamGanDayTheoThanhPho(int idCity,DateTime ngayCur)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var products = db.usp_SelectProductsRecentByCity(idCity,ngayCur);
            return products.ToArray<PRODUCT>();
        }

        public static PRODUCT[] DanhSachSanPhamSapToiTheoThanhPho(int idCity, DateTime ngayCur)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var products = db.usp_SelectProductsFutherByCity(idCity, ngayCur);
            return products.ToArray<PRODUCT>();
        }

        public static PRODUCT LaySanPhamCoID(int idProduct)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = db.usp_SelectProductsByID(idProduct);
            return (temp.ToArray<PRODUCT>())[0];            
        }

        public static PRODUCT LayRan1SanPhamCo(int idCity, DateTime ngay)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = db.usp_SelectRan1ProductsByDateANDCity(idCity, ngay);
            return (temp.ToArray<PRODUCT>())[0];
        }

        public static PRODUCT[] LayRan5SanPhamCo(int idCity,DateTime ngay)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            var temp = db.usp_SelectRan5ProductsByDateANDCity(idCity,ngay);
            return (temp.ToArray<PRODUCT>());
        }
        
        /// <summary>
        /// Đưa sản phẩm vào hàng đợi
        /// Nếu Quản lí muốn thêm sản phẩm vào danh sản sách khuyến mãi thì cập nhật lại trạng thái
        /// </summary>
        public static void AddProduct(PRODUCT prd)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            db.PRODUCTs.InsertOnSubmit(prd);
            db.SubmitChanges();
        }

        //Tuan
        public static Object DoanhThuTheoSanPham()
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            //var products = from p in db.PRODUCTs                             
            //               select new
            //               {
            //                   ID = p.IDPRODUCT,
            //                   NAME = p.SLOGAN,
            //                   PRICE = p.PRICE,
            //                   COUNT = p.ORDERs.Count,
            //                   REVENUE = p.ORDERs.Sum(a => a.PRICE * a.QUANTITY)
            //               };
            var products = db.usp_ComputeRevenueOfProducts();
            return products;
        }

        
        //BỔ SUNG BY TUY
        public static bool DeleteProduct(int id)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            PRODUCT product = db.PRODUCTs.Single(p => p.IDPRODUCT == id);
            db.PRODUCTs.DeleteOnSubmit(product);
            db.SubmitChanges();
            return true;
        }
        public static PRODUCT GetProductById(int id)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            return db.PRODUCTs.Single(p => p.IDPRODUCT == id);
        }

        public static bool UpdateProduct(PRODUCT product)
        {
            DataClassesNHOMMUADataContext db = new DataClassesNHOMMUADataContext();
            PRODUCT pr = db.PRODUCTs.Single(p => p.IDPRODUCT == product.IDPRODUCT);
            pr.IDPROVIDER = product.IDPROVIDER;
            pr.PRICE = product.PRICE;
            pr.RATE_DECREASE = product.RATE_DECREASE;
            pr.MIN_NUMBER = product.MIN_NUMBER;
            pr.MAX_NUMBER = product.MAX_NUMBER;
            pr.TIME_START = product.TIME_START;
            pr.TIME_STOP = product.TIME_STOP;
            pr.IDCITY = product.IDCITY;
            pr.STATUS = product.STATUS;
            pr.SLOGAN = product.SLOGAN;
            pr.CONDITION = product.CONDITION;
            pr.CONTENT = product.CONTENT;
            pr.HIGHLIGHT = product.HIGHLIGHT;
            db.SubmitChanges();
            return true;
        }

    }
}
